using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RemoveMine.Entities;


namespace RemoveMine.GUI
{
    public partial class UCOco : UserControl
    {
        public UCOco()
        {
            InitializeComponent();
        }
        
        protected Bitmap _picture;
        [Browsable (true)]
        public  Bitmap Picture
        {
            get { return _picture; }
            set { _picture = value; }
        }
        private OcoDTO _oco;

        internal OcoDTO Oco
        {
            get { return _oco; }
            set { _oco = value; }
        }

        private void UCOco_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            if (_picture != null)
            {
                int w = _picture.Width;
                int h = _picture.Height;
                int x = (Width - w) / 2;
                int y = (Height - h) / 2;
                g.DrawImage(_picture, x, y, w, h);
            }
        }

        private void UCOco_Load(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
        }
         private void UCOco_MouseUp(object sender, MouseEventArgs e)
        {
            string _duongdan = Application.StartupPath + "\\GiaTriOCo\\";

            if (frmMain._frmChinh.Vanchoi.TrangThai  == TTVanChoi.ChuaBatDau)
                if (e.Button == MouseButtons.Left)
                {
                    frmMain._frmChinh.Vanchoi.TrangThai  = TTVanChoi.BatDau;
                    frmMain._frmChinh.Bodinhthoi.Start();
                }

            if (frmMain._frmChinh.Vanchoi.TrangThai  == TTVanChoi.BatDau)
            {
                string _tenfile = "";
                if (e.Button == MouseButtons.Left && _oco.Hanhdong == HanhDong.ChuaMo)
                {
                    _tenfile = _oco.Giatri.ToString();
                    if (_oco.Giatri == GiaTriOCo.Bom)
                    {
                        _tenfile += "No";
                        frmMain._frmChinh.Vanchoi.TrangThai  = TTVanChoi.Thua;
                    }
                    _oco.Hanhdong = HanhDong.Mo;
                    _tenfile += ".bmp"; _duongdan += _tenfile;
                    _picture = new Bitmap(Bitmap.FromFile(_duongdan));
                    Invalidate();
                }
                if (_tenfile == "Zero.bmp")
                    frmMain._frmChinh.MoDeQui(_oco.Toado);
                if (frmMain._frmChinh.Vanchoi.TrangThai == TTVanChoi.Thua)
                {
                    frmMain._frmChinh.Bodinhthoi.Stop();
                    frmMain._frmChinh.XuLyThua();
                }
            }
        }

        private void UCOco_MouseDown(object sender, MouseEventArgs e)
        {
            string _duongdan = Application.StartupPath + "\\GiaTriOCo\\";
            string _tenfile;

            if (frmMain._frmChinh.Vanchoi.TrangThai != TTVanChoi.Thua)
            {
                if (e.Button == MouseButtons.Left && _oco.Hanhdong == HanhDong.ChuaMo)
                {
                    _tenfile = "Zero.bmp"; _duongdan += _tenfile;
                    _picture = new Bitmap(Bitmap.FromFile(_duongdan));
                    Invalidate();
                }

                if ((e.Button == MouseButtons.Right) && (_oco.Hanhdong != HanhDong.Mo))
                {
                    if (_oco.Hanhdong == HanhDong.ChuaMo)
                    {
                        if (frmMain._frmChinh.Vanchoi.Socodung > 0)
                        {
                            _oco.Hanhdong = HanhDong.DatCo;
                            frmMain._frmChinh.Vanchoi.Socodung--;
                            frmMain._frmChinh._panelHienThi.Invalidate();
                            _tenfile = _oco.Hanhdong.ToString();
                            _tenfile += ".bmp"; _duongdan += _tenfile;
                            _picture = new Bitmap(Bitmap.FromFile(_duongdan));
                        }
                    }
                    else
                        if (_oco.Hanhdong == HanhDong.DatCo)
                        {
                            _oco.Hanhdong = HanhDong.ChuaMo;
                            frmMain._frmChinh.Vanchoi.Socodung++;
                            frmMain._frmChinh._panelHienThi.Invalidate();
                            _tenfile = _oco.Hanhdong.ToString();
                            _tenfile += ".bmp"; _duongdan += _tenfile;
                            _picture = new Bitmap(Bitmap.FromFile(_duongdan));
                        }
                    Invalidate();
                }
            }
        }
        // click chuot (chuot trai = mo, chuot phai = dat co)
        //private void UCOco_MouseClick(object sender, MouseEventArgs e)
        //{
        //    string _duongdan = Application.StartupPath;
        //    string _tenfile;

        //    if (!frmMain._thua)
        //    {
        //        // mo o
        //        if ((e.Button == MouseButtons.Left) && (_oco.Hanhdong == HanhDong.ChuaMo))
        //        {
        //            _tenfile = _oco.Giatri .ToString();
        //            if (_oco.Trangthai == TrangThai.Bom)
        //            {
        //                _tenfile += "No";
        //                frmMain._thua = true;
        //            }
        //            _oco.Hanhdong = HanhDong.Mo;
        //            _tenfile += ".bmp";
        //            _duongdan += "\\"+ _tenfile;
        //            _picture = new Bitmap(Bitmap.FromFile(_duongdan));
        //            Invalidate();
        //        }
        //        // dat co
        //        if ((e.Button == MouseButtons.Right) && (_oco.Hanhdong != HanhDong.Mo))
        //        {
        //            if (_oco.Hanhdong == HanhDong.ChuaMo)
        //                _oco.Hanhdong = HanhDong.DatCo;
        //            else
        //                if (_oco.Hanhdong == HanhDong.DatCo)
        //                    _oco.Hanhdong = HanhDong.ChuaMo;
        //            _tenfile = _oco.Hanhdong.ToString(); 
        //            _tenfile += ".bmp"; _duongdan += "\\" + _tenfile;
        //            _picture = new Bitmap(Bitmap.FromFile(_duongdan));
        //            Invalidate();
        //        }
        //        if (frmMain._thua)
        //            frmMain._frmChinh.XuLyThua();
        //    }
        //}
        
    }
}
